window.onload = function () {
    window.vue = new Vue({
        el: '#my-div',
        data: {
            list: [],
            menu: '',
            icon: '',
            url: '',
            sortedIndex: '',
            index: '',
        },
        methods: {
            find: function () {
                doGet({
                    url: 'user/menu/query',
                    data: {},
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.list = res.data.data;
                            $('#table-main')[0].style.visibility = 'visible';
                        } else {
                            console.log(res);
                        }
                    }
                });
            },

            del: function (index) {
                window.parent.showConfirm('重要提示', '您确定要删除此记录吗?', function () {
                    let id = vue.list[index].id;
                    vue.index = index;
                    doPost({
                        url: 'user/menu/delete',
                        data: {
                            id: id,
                        },
                        callback: function (res) {
                            if (res.data.code === 1) {
                                vue.list.removeAt(vue.index);
                                vue.list = vue.list.clone();
                                window.parent.showSuccess();
                            } else {
                                window.parent.showInfo(res.data.msg);
                            }
                        }
                    });
                });
            },

            showAdd: function () {
                this.menu = '';
                this.icon = '';
                this.url = '';
                this.sortedIndex = '';
                this.id = '';
                this.index = -1;
                $('#update-modal').modal('show');
            },
            showUpdate: function (i) {
                this.menu = this.list[i].menu;
                this.icon = this.list[i].icon;
                this.url = this.list[i].url;
                this.sortedIndex = this.list[i].sorted_index;
                this.id = this.list[i].id;
                this.index = i;
                $('#update-modal').modal('show');
            },

            save: function () {
                let menus = '[]';
                doPost({
                    url: 'user/menu/update',
                    data: {
                        id: vue.id,
                        menu: vue.menu,
                        icon: vue.icon,
                        url: vue.url,
                        sorted_index: vue.sortedIndex,
                    },
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.find();
                            window.parent.showSuccess();
                            $('#update-modal').modal('hide');
                        } else {
                            window.parent.showInfo(res.data.msg);
                        }
                    }
                });
            },
            clone: function (id) {
                window.parent.showConfirm('操作提示', '您确定要克隆此条记录吗?', function () {
                    doPost({
                        url: 'user/menu/clone',
                        data: {
                            id: id,
                        },
                        callback: function (res) {
                            if (res.data.code === 1) {
                                vue.find();
                                window.parent.showSuccess();
                            } else {
                                window.parent.showInfo(res.data.msg);
                            }
                        }
                    });
                });
            },
        },
    });
    vue.find();

    $('#inputRole').bind('keypress', function (event) {
        if (event.keyCode === 13) {
            vue.save();
        }
    });

};
